package com.common.app.utls.network

import com.common.app.data.bean.KeyEvents
import com.common.base.app.extras.no
import com.common.base.data.CommonBean
import com.google.gson.Gson
import com.jeremyliao.liveeventbus.LiveEventBus
import okhttp3.Response

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/19
 * <p>
 * 描述：返回值判断，处理单点登陆、账号封禁
 * <p>
 * 修订历史：
 */
class ResponseInterceptor : ResInterceptor() {

    override fun intercept(response: Response, url: String, body: String): Response {
        // 检查请求是否有特定的header
        val skip = response.header("Skip-Interceptor") == "true"
        skip.no {
            if (response.isSuccessful) {
                try {
                    val tempRes = Gson().fromJson(body, CommonBean::class.java)
                    if (tempRes.Code == 1) {  // token失效过期
                        LiveEventBus.get(KeyEvents.EVENT_TOKEN_EXPIRED, String::class.java).post("!")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        return response
    }
}